package com.jvxb.manage.livable.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jvxb.common.web.RespMsg;
import com.jvxb.manage.livable.entity.RedisChangeData;
import com.jvxb.manage.livable.service.MysqlChangeDataService;
import com.jvxb.manage.livable.service.RedisChangeDataService;
import com.jvxb.manage.livable.service.dataAlert.MySQLDataAlertService;
import com.jvxb.manage.livable.service.dataAlert.RedisDataAlertService;
import com.jvxb.manage.livable.vo.DataAlterVo;
import com.jvxb.manage.livable.vo.DbGrammarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * @author jvxb
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/dataAlter/v1")
public class DataAlertController {

    @Autowired
    RedisDataAlertService redisDataAlertService;
    @Autowired
    RedisChangeDataService redisChangeDataService;
    @Autowired
    MysqlChangeDataService mysqlChangeDataService;
    @Autowired
    MySQLDataAlertService mySQLDataAlertService;

    /**
     * redis命令检测
     */
    @PostMapping("/check/mysql")
    public RespMsg checkMyql(@RequestBody DbGrammarVo dbGrammarVo) {
        Long clusterId = dbGrammarVo.getClusterId();
        String command = dbGrammarVo.getCommand();
        Assert.notNull(clusterId, "param clusterId must not be null");
        Assert.notNull(command, "param command must not be null");
        String result = mySQLDataAlertService.checkMysql(clusterId, command);
        return RespMsg.ok(result);
    }

    /**
     * redis命令检测
     */
    @PostMapping("/check/redis")
    public RespMsg checkRedis(@RequestBody DbGrammarVo dbGrammarVo) {
        Long clusterId = dbGrammarVo.getClusterId();
        String command = dbGrammarVo.getCommand();
        Assert.notNull(clusterId, "param clusterId must not be null");
        Assert.notNull(command, "param command must not be null");
        String result = redisDataAlertService.checkRedis(clusterId, command);
        return RespMsg.ok(result);
    }

    /**
     * 发起变更工单
     */
    @PostMapping("/createOrder/mysql")
    public RespMsg createOrderMysql(@RequestBody DataAlterVo dataAlterVo) {
        Assert.hasLength(dataAlterVo.getTitle(), "param title must not be null or empty");
        Assert.notNull(dataAlterVo.getClusterId(), "param clusterId must not be null");
        Assert.notNull(dataAlterVo.getCommand(), "param command must not be null");
        mySQLDataAlertService.createOrder(dataAlterVo);
        return RespMsg.ok();
    }

    /**
     * 发起变更工单
     */
    @PostMapping("/createOrder/redis")
    public RespMsg createOrderRedis(@RequestBody DataAlterVo dataAlterVo) {
        Assert.notNull(dataAlterVo.getClusterId(), "param clusterId must not be null");
        Assert.notNull(dataAlterVo.getCommand(), "param command must not be null");
        Assert.hasLength(dataAlterVo.getTitle(), "param title must not be null or empty");
        redisDataAlertService.createOrder(dataAlterVo);
        return RespMsg.ok();
    }

    /**
     * 获取redis变更工单中的命令
     */
    @GetMapping("/redisAlertCommand")
    public RespMsg redisAlertCommand(Long workOrderId,
                                     @RequestParam(defaultValue = "10") Integer size,
                                     @RequestParam(defaultValue = "1") Integer current) {
        Assert.notNull(workOrderId, "param workOrderId must not be null");
        IPage records = new Page<>(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(RedisChangeData.WORK_ORDER_ID, workOrderId);
        records = redisChangeDataService.page(records, wrapper);
        return RespMsg.ok(records);
    }

    /**
     * 获取mysql变更工单中的命令
     */
    @GetMapping("/mysqlAlertCommand")
    public RespMsg mysqlAlertCommand(Long workOrderId,
                                     @RequestParam(defaultValue = "10") Integer size,
                                     @RequestParam(defaultValue = "1") Integer current) {
        Assert.notNull(workOrderId, "param workOrderId must not be null");
        IPage records = new Page<>(current, size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(RedisChangeData.WORK_ORDER_ID, workOrderId);
        records = mysqlChangeDataService.page(records, wrapper);
        return RespMsg.ok(records);
    }

}
